package com.lmh.backend.dao.daoImpl;

import com.lmh.backend.dao.NewsDao;
import com.lmh.backend.entity.Author;
import com.lmh.backend.entity.News;
import com.lmh.backend.utils.JpaUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;

@Repository
public class NewsDaoImpl implements NewsDao {
    @Override
    public List<News> getAllNews() {
        EntityManager manager = JpaUtils.getEntityManager();
        Query query = manager.createQuery("select news from News news where news.isChecked = true");
        List<News> newsList = query.getResultList();
        manager.close();
        return newsList;
    }

    @Override
    public List<News> getWaitCheckNews() {
        EntityManager manager = JpaUtils.getEntityManager();
        Query query = manager.createQuery("select news from News news where news.isChecked = false");
        List<News> newsList = query.getResultList();
        manager.close();
        return newsList;
    }

    @Override
    public News getNewsById(Long newsId) {
        EntityManager manager = JpaUtils.getEntityManager();
        Query query = manager.createQuery("select news from News news where news.id = ?");
        query.setParameter(1, newsId);
        News news = (News) query.getSingleResult();
        manager.close();
        return news;
    }

    @Override
    public void passNews(Long newsId) {
        EntityManager manager = JpaUtils.getEntityManager();
        EntityTransaction tx = manager.getTransaction();
        tx.begin();
        Query query = manager.createQuery("select news from News news where news.id = ?");
        query.setParameter(1, newsId);
        News news = (News) query.getSingleResult();
        news.setChecked(true);
        tx.commit();
        manager.close();
    }


    @Override
    public void delNews(Long newsId) {
        EntityManager manager = JpaUtils.getEntityManager();
        EntityTransaction tx = manager.getTransaction();
        tx.begin();
        Query query = manager.createQuery("select news from News news where news.id = ?");
        query.setParameter(1, newsId);
        News news = (News) query.getSingleResult();
        manager.remove(news);
        tx.commit();
        manager.close();
    }

    @Override
    public void editNews(Long newsId, String new_title, String new_content) {
        EntityManager manager = JpaUtils.getEntityManager();
        EntityTransaction tx = manager.getTransaction();
        tx.begin();
        News news = manager.find(News.class, newsId);
        news.setTitle(new_title);
        news.setContent(new_content);
        news.setChecked(false);
        tx.commit();
        manager.close();
    }

    @Override
    public void save(News news) {
        EntityManager manager = JpaUtils.getEntityManager();
        EntityTransaction tx = manager.getTransaction();
        tx.begin();
        manager.persist(news);
        tx.commit();
        manager.close();
    }

}
